var express = require('express');
var router = express.Router();
var chatDB = require('../models/chat_datas');

router.get('/saveChat', function (req, res, next) {
    // let history = req.body.history
    // let roomId = req.body.roomId
    let history = [
        {
            msg: "msg1",
            roomId: "000",
            timeStamp: 12345,
            username: "admin"
        },
        {
            msg: "msg2",
            roomId: "000",
            timeStamp: 12345,
            username: "admin2"
        }];
    let roomId = "room1";

    new chatDB({
        room_id: roomId,
        timeStamp: Date.now(),
        history: history
    }).save(err => {
        if (err) {
            res.json({
                status: "0",
                msg: err.message,
                result: {
                }
            });
        }
        else {
            res.json({
                status: "0",
                msg: 'save success',
                result: {

                }
            });
        }
    })


});


router.get('/getChat', function (req, res, next) {
    // let history = req.body.history
    // let roomId = req.body.roomId
    let roomId = "room1";

    chatDB.findOne({ room_id: roomId }, (err, doc) => {
        if (err) {
            res.json({
                status: "0",
                msg: err.message,
                result: {
                }
            });
        }
        else {
            if (doc) {
                res.json({
                    status: "1",
                    msg: "getChat success",
                    result: {
                        chatHistory: doc
                    }
                })
            }
            else {
                res.json({
                    status: "0",
                    msg: "history not exist",
                    result: {
                        chatHistory: undefined
                    }
                })
            }
        }
    })


});

// params:
// 根据房间号和用户名保存聊天记录
//  history: 聊天历史记录 Array
//  roomId: 房间号 String
//  username: 用户名 String

// return:
//   success or err

router.post('/saveChat', function (req, res, next) {
    // let history = req.body.history
    // let roomId = req.body.roomId
    // let history = [
    //     {
    //         msg: "msg1",
    //         roomId: "000",
    //         timeStamp: 12345,
    //         username: "admin"
    //     },
    //     {
    //         msg: "msg2",
    //         roomId: "000",
    //         timeStamp: 12345,
    //         username: "admin2"
    //     }];
    let history = req.body.history;
    let roomId = req.body.roomId;
    let username = req.body.username
    new chatDB({
        username: username,
        room_id: roomId,
        timeStamp: Date.now(),
        history: history
    }).save(err => {
        if (err) {
            res.json({
                status: "0",
                msg: err.message,
                result: {
                }
            });
        }
        else {
            res.json({
                status: "0",
                msg: 'save success',
                result: {

                }
            });
        }
    })
});

// params:
// 根据房间号和用户名寻找唯一对应的记录
//  roomId: 房间号 String
//  username: 用户名 String

// return:
//   chatHistory:
//         contantSchema: {
//             "msg": {
//                 type: String,
//             },
//             "timeStamp": {
//                 type: Number
//             },
//             "username": {
//                 type: String
//             }
//          }
//         chatSchema: {
//             "username": {
//                 type: String,
//                 require: true
//             },
//             "room_id": {
//                 type: String,
//                 required: true
//             },
//             "timeStamp": {
//                 type: Number
//             },
//             "history": {
//                 type: [contantSchema],
//             },
//         }
router.post('/getChat', function (req, res, next) {
    // let history = req.body.history
    // let roomId = req.body.roomId
    let roomId = req.body.roomId;
    let username = req.body.username;
    chatDB.findOne({ username: username, room_id: roomId }, (err, doc) => {
        if (err) {
            res.json({
                status: "0",
                msg: err.message,
                result: {
                }
            });
        }
        else {
            if (doc) {
                res.json({
                    status: "1",
                    msg: "getChat success",
                    result: {
                        chatHistory: doc
                    }
                })
            }
            else {
                res.json({
                    status: "0",
                    msg: "history not exist",
                    result: {
                        chatHistory: undefined
                    }
                })
            }
        }
    })


});

module.exports = router;